package com.simplenix.nicasio.navig;

import com.simplenix.nicasio.def.Module;
import com.simplenix.nicasio.misc.AppException;
import com.simplenix.nicasio.misc.FileUtil;
import com.simplenix.nicasio.misc.StrUtil;
import com.simplenix.nicasio.sys.SystemDef;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author fronald
 */
public class TrayManager {

	List<TrayItem> items;

	private static TrayManager tray;

	public static TrayManager getInstance() throws AppException {
		if (tray == null) {
			tray = new TrayManager();
		}
		return tray;
	}

	private TrayManager() throws AppException {
		try {
			this.items = new ArrayList<TrayItem>();
			for (Module m : SystemDef.getInstance().getModules().getModulesList()) {
				java.lang.String packName = m.getBasePackage();
				List<java.lang.String> classes = FileUtil.getClassList(packName + ".tray");
				for (java.lang.String className : classes) {
					Class c = Class.forName(className);
					TrayItem o = (TrayItem) c.newInstance();
					this.items.add(o);
				}
			}
		} catch (Exception e) {
			throw new AppException(e);
		}
	}

	public String getHtml(HttpServletRequest request) {
		List<String> htmls = new ArrayList<String>();
		for (TrayItem item : this.items) {
			String html = item.getHtml(request);
			if (!StrUtil.nvl(html).equals("")) {
				htmls.add(html);
			}
		}
		return StrUtil.listToString(htmls, " | ");
	}

}
